sum(c(2.2,4.1,2,pi))Tutoial - Part 2 TutoRial - Part 2
Marine Ecosystem Dynamics - 2024
Pipes
Pipes, expressed as %>% or |>, are very useful and make our code clearer. Using pipes, our data flow from one function to another.
Exercises
- Rewrite these chunks of code using the pipes
Solution
c(2.2,4.1,2,pi) |> sum()
# OR
c(2.2,4.1,2,pi) %>% sum()round(sum(c(2.2,4.1,2,pi)))
Solution
c(2.2,4.1,2,pi) |> sum() |> round()
# OR
c(2.2,4.1,2,pi) %>% sum() %>% round()round(sum(c(2.2,4.1,2,pi)), digits = 3)
Solution
c(2.2,4.1,2,pi) |> sum() |> round(digits = 3)
# OR
c(2.2,4.1,2,pi) %>% sum() %>% round(digits = 3)Tidy the data with tidyr
As seen in the slides, a tidy table has:
- Each variable in its own column
- Each observation in its own row
To reach this, tidyr has 4 key functions:
pivot_longerpivot_wideruniteseparate
Exercises
- If this is not done yet, download the dataset
zooplankton_seasonality.csv
Import the dataset in your environment
Is this dataset a tidy dataset?
Solution
| Month_abb | Year | Station | Coordinates | Group | Taxa | Biomass |
|---|---|---|---|---|---|---|
| Jan | 2009 | BY15 | 20.05000/57.33333 | Copepoda | Acartia | 6.650319 |
| Jan | 2009 | BY31 | 18.23333/58.58812 | Copepoda | Acartia | 1.816994 |
| Jan | 2009 | BY5 | 15.98333/55.25000 | Copepoda | Acartia | 5.562097 |
| Jan | 2009 | BY15 | 20.05000/57.33333 | Copepoda | Centropages | 5.738562 |
| Jan | 2009 | BY31 | 18.23333/58.58812 | Copepoda | Centropages | 1.228759 |
| Jan | 2009 | BY5 | 15.98333/55.25000 | Copepoda | Centropages | 14.405224 |
Each variable has its own column
Each variable has its own row
Coordinates has 2 values
- Separate the column
Coordinatesin 2 news columns:LongitudeandLatitude
Solution
| Month_abb | Year | Station | Longitude | Latitude | Group | Taxa | Biomass |
|---|---|---|---|---|---|---|---|
| Jan | 2009 | BY15 | 20.05000 | 57.33333 | Copepoda | Acartia | 6.650319 |
| Jan | 2009 | BY31 | 18.23333 | 58.58812 | Copepoda | Acartia | 1.816994 |
| Jan | 2009 | BY5 | 15.98333 | 55.25000 | Copepoda | Acartia | 5.562097 |
| Jan | 2009 | BY15 | 20.05000 | 57.33333 | Copepoda | Centropages | 5.738562 |
| Jan | 2009 | BY31 | 18.23333 | 58.58812 | Copepoda | Centropages | 1.228759 |
| Jan | 2009 | BY5 | 15.98333 | 55.25000 | Copepoda | Centropages | 14.405224 |
library(tidyr)
zooplankton |> separate(Coordinates, into = c("Longitude", "Latitude"), sep = "/")